package breeze.desk.picmodel.widgets;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.widget.RemoteViews;
import breeze.desk.picmodel.R;
import breeze.desk.picmodel.fragment.AppPreference;
import breeze.desk.picmodel.utils.PreferenceUtils;
import java.io.File;
import brz.breeze.bitmap_utils.BBitmapUtils;
import brz.breeze.app_utils.BAppUtils;

public class AppWidgetForth extends AppWidgetProvider{
    
    /*
	*@author BREEZE
	*@date 2021-06-11 22:27:55
    */
	
	@Override
	public void onReceive(Context context, Intent intent) {
		if(AppPreference.UPDATE_WIDGET.equalsIgnoreCase(intent.getAction())){
			update(context);
		}
		super.onReceive(context, intent);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		update(context);
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

	private void update(final Context context){
		String filepath = AppPreference.getPicFile(context,AppPreference.FILE_NANE_WIDGET_FORTH);
		if(new File(filepath).exists()){
			AppWidgetManager awm = AppWidgetManager.getInstance(context);
			RemoteViews rm = new RemoteViews(context.getPackageName(),R.layout.view_table_clock);
			ComponentName cn = new ComponentName(context,AppWidgetForth.class);
			rm.setTextColor(R.id.view_clock_text_1,Color.parseColor(PreferenceUtils.getStringValue(context,"textColor","#000000")));
			rm.setTextColor(R.id.view_clock_text_2,Color.parseColor(PreferenceUtils.getStringValue(context,"textColor","#000000")));
			Bitmap bitmap = BitmapFactory.decodeFile(filepath);
			rm.setImageViewBitmap(R.id.view_clock_image,BBitmapUtils.getRoundedBitmap(bitmap,1000));
			awm.updateAppWidget(cn,rm);
		}
	}

    
}
